# 修复SSH密钥文件权限
# 使用方法: .\fix_key_permissions.ps1 -KeyPath "C:\path\to\key.pem"

param(
    [Parameter(Mandatory=$true)]
    [string]$KeyPath
)

Write-Host "🔧 修复SSH密钥文件权限..." -ForegroundColor Cyan
Write-Host "文件路径: $KeyPath" -ForegroundColor Yellow

# 检查文件是否存在
if (!(Test-Path $KeyPath)) {
    Write-Host "❌ 文件不存在: $KeyPath" -ForegroundColor Red
    return
}

try {
    # 移除继承权限
    Write-Host "📝 移除继承权限..." -ForegroundColor Yellow
    icacls "$KeyPath" /inheritance:r | Out-Null
    
    # 设置只有当前用户有读取权限
    Write-Host "📝 设置权限..." -ForegroundColor Yellow
    $username = $env:USERNAME
    icacls "$KeyPath" /grant "${username}:(R)" | Out-Null
    
    # 验证权限
    Write-Host "✅ 权限修复完成！" -ForegroundColor Green
    Write-Host ""
    Write-Host "📋 当前权限:" -ForegroundColor Cyan
    icacls "$KeyPath"
    
} catch {
    Write-Host "❌ 修复失败: $_" -ForegroundColor Red
}

